package com.sky.aion;

import com.sky.aion.dd.DD;
import com.sky.aion.redis.JedisUtils;
import com.sky.aion.utils.StatusEnum;
import com.sky.aion.winio.WinIoUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogView {

    private volatile long lastTimeFileSize = 0; //上次文件大小int
    private int i=0;//计数器行数
    private int allHurt=0;//伤害总数
    private boolean logflag=true;//日志显示
    /**
     * 需要分析的日志标签
     */
    private static String[] CHENMOCheck = {"受到(.*?)的影响，陷入了沉默状态","处于沉默状态，无法使用魔法技能"};
    private static String[] jiYunCheck = {"受到(.*?)的影响，陷入了晕厥状态"};
    private static String[] jiansu = {"受到(.*?)的影响，陷入了无法移动状态","受到(.*?)结冰(.*?)的影响","受到(.*?)火笼(.*?)的影响"};


    private static String[] hurtCheck = {"受到(.*?)使用(.*?)受到了(.*?)的伤害"};
    private static String[] huibiCheck = {"使用集中回避 I技能","使用回避之契约 I技能","使用警戒姿态 I技能"};
    private static String[] dieCheck = {"您已经死亡"};
    //强化技能
    private static String[] dunCheck = {": 使用钢铁护膜 V技能",": 使用强化之眼 I技能",": 使用攻击之眼 I技能"};
    private static String[] gongzhuangtaiCheck = {": 使用强化之眼 I技能",": 使用攻击之眼 I技能"};

    //状态
    private static String[] zhuangtai = {"(.*?)已使用",": 使用(.*?)技能，"};




    /**
     * 实时输出日志信息
     *
     * @param logFile 日志文件
     * @throws IOException
     */
    public void showLog(File logFile) throws IOException {
        //在redis中读取上次的记录
        String strFileSize=JedisUtils.getKey(AionConstant.LASTTIMEFILESIZE);

        if (StringUtils.isNotBlank(strFileSize)&&AionConstant.ISLASTSIZEFLAG){
            lastTimeFileSize=Integer.parseInt(strFileSize);
            System.out.println("上次记录行数："+lastTimeFileSize);

        }

        //指定文件可读可写
        final RandomAccessFile randomFile = new RandomAccessFile(logFile, "r");

        //启动一个线程每1秒钟读取新增的日志信息
        ScheduledExecutorService exec =
                Executors.newScheduledThreadPool(1);
        exec.scheduleWithFixedDelay(new Runnable() {
            public void run() {
                try {
                    //获得变化部分的
                    randomFile.seek(lastTimeFileSize);
                    String line = "";

                    Set<String> nameSet=new HashSet<>();//攻击过我的人
                    while ((line =randomFile.readLine()) != null) {
                        //加血信息初始化
                        if (i>800){
                            i=0;
                            allHurt=0;
                            //写入redis记录行数
                            //JedisUtils.setKey(AionConstant.LASTTIMEFILESIZE,""+lastTimeFileSize);
                        }

                        if (nameSet.size()>10) nameSet.clear();//重置集合

                        line=new String(line.getBytes("ISO-8859-1"),"GBK");
                        if (StringUtils.isEmpty(line)) {
                            continue;
                        }
                        //伤害统计
                        i++;
                        int hurtTmp=countHurt(line,nameSet);
                        if (hurtTmp>0){
                            if (hurtTmp>1000){
                                //单次伤害加血
                                DD.INSTANCE.DD_str(StatusEnum.MANJIA.getKey());
                                //WinIoUtils.execu(StatusEnum.MANJIA.getKey());
                                JedisUtils.lpush(AionConstant.SPEAKLIST, StatusEnum.MANJIA.getMsg());
                            }
                            System.out.println(line+" 伤害统计："+hurtTmp);
                        }
                        allHurt+=hurtTmp;
                        String tmp;
                        if(allHurt>3500){
                            //总计伤害提醒
                            DD.INSTANCE.DD_str(StatusEnum.JIAXUE.getKey());
                            JedisUtils.lpush(AionConstant.SPEAKLIST, StatusEnum.JIAXUE.getMsg());
                            allHurt=0;
                        }else if (contains(line,CHENMOCheck)){
                            //沉默提醒
                            DD.INSTANCE.DD_str(StatusEnum.CHENME.getKey());
                            JedisUtils.lpush(AionConstant.SPEAKLIST, StatusEnum.CHENME.getMsg());

                        }else if (contains(line,jiYunCheck)){
                            //击晕提醒
                            DD.INSTANCE.DD_str(StatusEnum.JIYUN.getKey());
                            JedisUtils.lpush(AionConstant.SPEAKLIST, StatusEnum.JIYUN.getMsg());

                        }else if (contains(line,jiansu)){
                            //减速提醒
                            DD.INSTANCE.DD_str(StatusEnum.JIANSU.getKey());
                            JedisUtils.lpush(AionConstant.SPEAKLIST, StatusEnum.JIANSU.getMsg());

                        }else if (!(tmp=isHuiBi(line,nameSet)).equals("")){
                            //回避提醒
                            JedisUtils.lpush(AionConstant.SPEAKLIST, StatusEnum.HUIBI.getMsg()+tmp);

                        }else if(isDie(line)){
                            //角色死亡重置集合
                            nameSet.clear();//重置集合
                            allHurt=0;
                            JedisUtils.lpush(AionConstant.SPEAKLIST, StatusEnum.DIE.getMsg());
                            JedisUtils.delAll();

                        }/*else if(!(tmp=isZhuangTai(line)).equals("")){
                            System.out.println(tmp);

                            //状态提醒
                            for (StatusEnum speakEnum: StatusEnum.values()){
                                if (speakEnum.getRedisExpire()>0&&tmp.contains(speakEnum.getMsg())){
                                    JedisUtils.setExpireKey(speakEnum);
                                    break;
                                }
                            }

                        }*/else {
                            logflag=false;
                        }

                        if (AionConstant.SHOWLOG||logflag){
                            System.out.println(line);
                        }else{
                            logflag=true;
                        }


                    }
                    lastTimeFileSize = randomFile.length();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }, 0, 1, TimeUnit.SECONDS);
    }




    private static boolean isChenMo(String str) {
        return contains(str,CHENMOCheck);
    }

    private static boolean isJiYun(String str) {
        return contains(str,jiYunCheck);
    }

   /* private static boolean isJiDao(String str) {
        return contains(str,jiDaoCheck);
    }*/

    private static boolean isDie(String str) {
        return contains(str,dieCheck);
    }

    private static boolean isDun(String str) {
        return contains(str,dunCheck);
    }

    private static boolean isgong(String str) {
        return contains(str,gongzhuangtaiCheck);
    }

    private static String isHuiBi(String str,Set<String> set) {
        for (String name:set){
            for (String huibi : huibiCheck) {
                if (str.contains(name+huibi)) {
                    return name;
                }
            }
        }

        return "";
    }


    private static int countHurt(String str,Set<String> set) {

        for (String hurt:hurtCheck){
            Pattern pattern = Pattern.compile(hurt);// 匹配的模式
            Matcher m = pattern.matcher(str);
            while(m.find()){
                set.add(m.group(1));
                return Integer.parseInt(m.group(3).replace(",",""));
            }
        }

        return 0;
    }



    public static boolean contains(String str,String[] checks){
        for (String ck:checks ){
            /*if (str.contains(ck)){
                return true;
            }*/
            Pattern pattern = Pattern.compile(ck);// 匹配的模式
            Matcher m = pattern.matcher(str);
            while(m.find()){
               return true;
            }
        }
        return false;
    }



    private static String isZhuangTai(String str) {
        for (String zhuangtai : zhuangtai) {
            Pattern pattern = Pattern.compile(zhuangtai);// 匹配的模式
            Matcher m = pattern.matcher(str);
            while(m.find()){
                return m.group(1);
            }
        }
        return "";
    }




}
 